package cn.jianyongjie.org.controller;

import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.QueryResult;
import cn.jianyongjie.org.domain.SystemDetail;
import cn.jianyongjie.org.domain.SystemDict;
import cn.jianyongjie.org.query.SystemDetailQuery;
import cn.jianyongjie.org.service.ISystemDetService;
import cn.jianyongjie.org.service.ISystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: TODO
* @Author: Jian Yong Jie
* @Version: 1.0
* @Date:  2022/4/22 9:50
*/
@RestController
@RequestMapping("/systemDet")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class SystemDetController {

    @Autowired
    private ISystemDetService iSystemDetService;

    @Autowired
    private ISystemService iSystemService;

    /*
    * @Title: SystemDetailController
    * @Description: 查询所有员工
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/23 16:58
    * @Parameters: * @param null
    * @Return
    */
    @GetMapping
    public List<SystemDetail> findSystemDetails(){
        return iSystemDetService.loadAll();
    }



    /*
    * @Title: SystemDetailController
    * @Description: 根据id查询数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 9:55
    * @Parameters: * @param id
    * @Return cn.jianyongjie.org.domain.SystemDetail
    */
    // 使用restful风格，设置请求
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SystemDetail findSystemDetailById(@ApiParam(name="ID属性",required = true) @PathVariable("id") long id){

        return iSystemDetService.loadById(id);
    }




    /*
    * @Title: SystemDetailController
    * @Description: 分页查询
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 10:01
    * @Parameters: * @param
    * @Return cn.jianyongjie.basic.utils.QueryResult
    */
    @PostMapping
    public QueryResult<SystemDetail> findemployees(@RequestBody SystemDetailQuery employeeQuery){
        return iSystemDetService.queryData(employeeQuery);
    }

    /*
    * @Title: SystemDetailController
    * @Description: 根据id删除数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 12:30
    * @Parameters: * @param
    * @Return void
    */
    @DeleteMapping("/{id}")
    public AjaxResult delSystemDetail(@PathVariable("id") Long id){
        try {
            // 根据id删除数据
            iSystemDetService.remove(id);
            
            // 返回Ajax
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: SystemDetailController
    * @Description: 新增或者修改
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/25 12:25
    * @Parameters: * @param employee
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody SystemDetail employee){
        try {
            System.out.println(employee);
            // 根据id进行新增或者修改
            if (employee.getId()==null){
                // 进行新增
                iSystemDetService.save(employee);
            }else {
                // 进行修改
                iSystemDetService.update(employee);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: SystemDetailController
    * @Description: 批量删除
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/23 12:32
    * @Parameters: * @param ids
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PatchMapping
    public  AjaxResult delSystemDetails(@RequestBody List<Long> ids){
        try {
            iSystemDetService.batchRemove(ids);
            return new AjaxResult();
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

}
